﻿$(function () {
    $("#pathway").ligerComboBox({
        width: 140,
        data: [{ "ID": -1, "Name": "全部" }, { "ID": 0, "Name": "CEA 、 CAS" }, { "ID": 1, "Name": "静脉溶栓、血管内介入" }, { "ID": 2, "Name": "脑出血" }, { "ID": 3, "Name": "颅内动脉瘤" }],
        isMultiSelect: false,
        valueField: "ID",
        textField: "Name",
        value: -1,
        valueFieldID: 'hdnpathway',
    });
    //$("#status").ligerComboBox({
    //    width: 80,
    //    data: [{ "ID": -1, "Name": "全部" }, { "ID": 0, "Name": "院前" }, { "ID": 1, "Name": "在科" }],
    //    isMultiSelect: false,
    //    valueField: "ID",
    //    textField: "Name",
    //    value: -1,
    //    valueFieldID: 'hdnstatus',
    //});
    //来院方式
    $("#comeType").ligerComboBox({
        width: 110,
        data: [{ "ID": -1, "Name": "全部" }, { "ID": 0, "Name": "本院急救车" }, { "ID": 1, "Name": "当地120" }, { "ID": 2, "Name": "外院转院" }, { "ID": 3, "Name": "自行来院" }],
        isMultiSelect: false,
        valueField: "ID",
        textField: "Name",
        value: -1,
        valueFieldID: 'hdncomeType',
    });

    $("#txtStartDate,#txtEndDate").ligerDateEditor({
        showTime: false,
        format: "yyyy-MM-dd",
        width: 90,
        cancelable: false
    });

    $('#btnSearch').click(function () {
        search(1, $("select[name=rp]").val());
    });
    $("#btnPrintWd").on("click", function () {
        if ($("#txtSelectedpId").val() == 0) { $.ligerDialog.alert('请选择要操作的患者信息'); return; }
        var tit = "腕带打印";
        $.ligerDialog.open({
            height: 400,
            width: 700,
            title: tit,
            url: '/StrokeCenter/PatientList/WristPrint?pid=' + $("#txtSelectedpId").val(),
            showMax: false,
            showToggle: false,
            showMin: false,
            isResize: true,
            slide: false,
            data: {},
            buttons: [{
                text: '回调函数', onclick: function (i, d) {
                    $("#btnSearch").click();
                }
            }],
            cls: "closeCls"
        }).max();
        $(".l-dialog-buttons").hide();
    });
    $('#btnSearch').click();

    $('#btnGiveup').click(function () {
        if ($("#txtSelectedpId").val() == 0) { $.ligerDialog.alert('请选择要操作的患者信息'); return; }
        $.ligerDialog.confirm('确定作废选定患者吗', function (yes) {
            if (yes) {
                var url = "/StrokeCenter/PatientList/GiveUp";
                $.get(url, {
                    rnd: new Date().getMilliseconds(),
                    patientId: $("#txtSelectedpId").val()
                }, function (data, textStatus) {
                    if (textStatus == "success") {
                        var d = $.parseJSON(data);
                        if (d.Result == 1) {
                            $.ligerDialog.success("操作成功");
                            $('#btnSearch').click();
                        }
                        else if (d.Result == 0) {
                            $.ligerDialog.error(d.Body);
                        }
                        else {
                            $.ligerDialog.error("程序错误");
                        }
                    }
                })
            }
        });
    });
    $('#btnAdd').click(function () {
        edit(0);
    });
    $("#btnEdit").on("click", function () {
        if ($("#txtSelectedpId").val() == 0) { $.ligerDialog.alert('请选择要操作的患者信息'); return; }
        edit($("#txtSelectedpId").val());
    });
});
function opennewtab(patientId, patientName) {
    window.parent.top.tab.addTabItem({ tabid: patientId, text: "卒中病历", url: "/StrokeCenter/MedicalRecords/Index?patientId=" + patientId });
}
function search(pageNumber, pageSize) {
    var queryData = {
        id: "",
        name: $("#txtName").val(),//名字
        status: $("#hdnstatus").val(),//状态
        pathway: $("#hdnpathway").val(),//诊断
        comeType: $("#hdncomeType").val(),//来院方式
        startDate: $("#txtStartDate").val(),
        endDate: $("#txtEndDate").val(),
    }
    initTable(queryData, 1, pageSize);
}
function initTable(queryData, pageNumber, pageSize) {
    $("#maingrid").ligerGrid({
        height: '100%',
        url: '/PatientList/Search?ram=' + new Date().getMilliseconds(),
        parms: queryData,
        page: pageNumber,
        pageSize: IsEmpty(pageSize) ? 20 : pageSize,
        rownumbers: false,
        heightDiff: -5,
        columns: [
{ display: '就诊ID', name: 'PATIENT_ID', minWidth: 50, width: 100 },
{
    display: '姓名', name: 'PATIENT_NAME', minWidth: 100, width: 100, render: function (row) {
        return "<a style=\"cursor:pointer;\" onclick=\"opennewtab('" + row.PATIENT_ID + "','" + row.PATIENT_NAME + "')\" >" + row.PATIENT_NAME + "</a>";
    }
},
{
    display: '性别', name: 'PATIENT_SEX', minWidth: 50, width: 50,
    render: function (rowData) {
        if (typeof (rowData.PATIENT_SEX) == "undefined" || rowData.PATIENT_SEX == null) return "未知";
        if (rowData.PATIENT_SEX == "1") return "男";
        if (rowData.PATIENT_SEX == "0") return "女";
        return rowData.PATIENT_SEX;
    }
},
{ display: '年龄', name: 'PATIENT_AGE', minWidth: 50, width: 50 },
{
    display: '腕带状态', name: 'RFID_State', minWidth: 100, width: 80,
    render: function (rowData) {
        if (typeof (rowData.RFID_State) == "undefined" || rowData.RFID_State == null) return "未知";
        if (rowData.RFID_State == "0") return "未绑定";
        if (rowData.RFID_State == "1") return "<span style=\"color:green\">已绑定</span>";
        return "<span title=\"" + rowData.RFID_State + "\">未知</span>";
    }
},
//{
//    display: '患者状态', name: 'PATIENT_STATUS', minWidth: 50, width: 50,
//    render: function (rowData) {
//        if (rowData.PATIENT_STATUS == "0") return "院前";
//        if (rowData.PATIENT_STATUS == "1") return "在科";
//    }
//},
{
    display: '绿色通道', name: 'IsGreenChannel', minWidth: 50, width: 80,
    render: function (rowData) {
        if (typeof (rowData.IsGreenChannel) == "undefined" || rowData.IsGreenChannel == null || rowData.IsGreenChannel == "0")
            return "<span onclick=\"StartGreenChannel(" + rowData.PATIENT_ID + ")\" style=\"cursor:pointer\">未启动</span>";
        if (rowData.IsGreenChannel == "1")
            return "<span style=\"color:green\">已启动</span>";
        return "<span onclick=\"StartGreenChannel(" + rowData.PATIENT_ID + ")\" style=\"cursor:pointer\">未启动</span>";
    }
},
{
    display: '数据上报', name: 'Reviewers_State', minWidth: 50, width: 80,
    render: function (rowData) {
        var value = "";
        if (rowData.Reviewers_State == 9) {
            value = "未审核";
        }
        else if (rowData.Reviewers_State == 0) {
            value = "审核不通过";
        }
        else if (rowData.Reviewers_State == 1) {
            value = "审核通过";
        }
        else if (rowData.Reviewers_State == 2) {
            value = "<font style='color:red;'>验证异常<font>";
        }
        else if (rowData.Reviewers_State == 3) {
            value = "上报中";
        }
        else if (rowData.Reviewers_State == 4) {
            value = "<font style='color:red;'>上报失败<font>";
        }
        else if (rowData.Reviewers_State == 5) {
            value = "上报成功";
        }
        return value;
    }
},
{
    display: '到达医院时间', name: 'EMS_ARRIVEDOOR_TIME', minWidth: 150, width: 180,
    render: function (rowData) {
        var time = rowData.EMS_ARRIVEDOOR_TIME;
        return obj2Timemm(time);
    }
},
{
    display: '卒中诊断', name: 'Stroke_Diagnosis', minWidth: 150,
    render: function (rowData) {
        if (rowData.Stroke_Diagnosis == "0") return "CEA 、 CAS";
        if (rowData.Stroke_Diagnosis == "1") return "静脉溶栓、血管内介入";
        if (rowData.Stroke_Diagnosis == "2") return "脑出血";
        if (rowData.Stroke_Diagnosis == "3") return "颅内动脉瘤";
        return "未知";
    }
},
{
    display: '来院方式', name: 'PATIENT_ARRIVE_CLASS', minWidth: 100,
    render: function (rowData) {
        if (rowData.PATIENT_ARRIVE_CLASS == "0") return "本院急救车";
        if (rowData.PATIENT_ARRIVE_CLASS == "1") return "当地120";
        if (rowData.PATIENT_ARRIVE_CLASS == "2") return "外院转院";
        if (rowData.PATIENT_ARRIVE_CLASS == "3") return "自行来院";
        return "不详";
    }
},
{
    display: '入院途径', name: 'AdmissionPath', minWidth: 150,
    render: function (rowData) {
        if (rowData.AdmissionPath == "0") return "急诊";
        if (rowData.AdmissionPath == "1") return "门诊";
        if (rowData.AdmissionPath == "2") return "其他医疗机构转入";
        if (rowData.AdmissionPath == "3") return "其他";
        return "未知";
    }
}
        ],
        isScroll: true,
        onSuccess: function (data, grid) {
            $("#spanPatientNum").text(data.Total);
            $("#spanInHospitalNum").text(data.InHospitalNum);
            $("#spanPreHospitalNum").text(data.PreHospitalNum);
            $("#spanPathWayNum").text(data.PathWayNum);
        },
        onSelectRow: function (data, rowindex, rowobj) {
            $("#txtSelectedpId").val(data.PATIENT_ID);
        }
    });
}
function StartGreenChannel(patientId) {
    $.ligerDialog.confirm('确定启动绿色通道', function (yes) {
        if (yes) {
            $.post("/StrokeCenter/GreenChannel/StartGreenChannel", {
                rnd: new Date().getMilliseconds(),
                patientId: patientId
            }, function (data, textStatus) {
                if (textStatus == "success") {
                    var d = $.parseJSON(data);
                    if (d.Result == 1) {
                        $.ligerDialog.success("启动成功");
                        $('#btnSearch').click();
                    }
                    else if (d.Result == 0) {
                        $.ligerDialog.error(d.Body);
                    }
                    else {
                        $.ligerDialog.error("程序错误");
                    }
                }
            })
        }
    });
}
function showPath(id) {
    $.ligerDialog.open({
        height: 650,
        width: 420,
        title: '路径总览',
        url: '/StrokeCenter/Pathway/Index?id=' + id,
        showMax: true,
        showToggle: false,
        showMin: false,
        isResize: false,
        slide: false,
        data: {},
        buttons: [{
            text: '回调函数', onclick: function (i, d) {
                $("#btnSearch").click();
            }
        }],
        cls: "closeCls"
    }).max();
    $(".l-dialog-buttons").hide();
}
function edit(id) {
    var tit = (!IsEmpty(id) && parseInt(id) > 0) ? "编辑患者" : "新增患者";
    $.ligerDialog.open({
        //解决此内核无法显示日历控件问题
        height: (navigator.userAgent.toString().indexOf("WebKit") > 0) ? 600 : 400,
        width: 520,
        title: tit,
        url: '/StrokeCenter/PatientList/Edit?Id=' + id,
        showMax: false,
        showToggle: false,
        showMin: false,
        isResize: false,
        slide: false,
        data: {},
        buttons: [{
            text: '回调函数', onclick: function (i, d) {
                $("#btnSearch").click();
            }
        }],
        cls: "closeCls"
    }).max();
    $(".l-dialog-buttons").hide();
}
$(document).on("click", ".closeCls", function () {
    $("#btnSearch").click();
});